import Koa from 'koa';
import {initMySql} from './SQLmodules/index';
import koabody from 'koa-body';
import router from './Routers/main';
import {initClients} from './Client/main';

let app = new Koa();
let server = require('http').Server(app.callback());

app.use(async (ctx, next)=>{
	return next().then(()=>{
		ctx.body = ctx.body || {}
		ctx.body.ok = ctx.body.ok || 1
		ctx.body['from'] = ctx.request.url;
	}, err=>{
		ctx.body = {
			status: err.status,
			message: err.message
		} 
	})
})

app.use(koabody({}));

app.use((ctx, next)=>{
	if (ctx.request.method === 'GET'){
		ctx.request.body = ctx.request.query;
	}
	return next();
})

app.use(router.routes());

initClients(server);

initMySql().then(()=>{
	console.log("Database Ok.");
	server.listen(3000);
})


